package com.zjff.mobiletwo.service.impl;

import com.zjff.mobiletwo.dao.AddressDao;
import com.zjff.mobiletwo.dao.OrderDao;
import com.zjff.mobiletwo.entity.AddressEntity;
import com.zjff.mobiletwo.entity.OrderEntity;
import com.zjff.mobiletwo.entity.OrderItemEntity;
import com.zjff.mobiletwo.entity.ResultEntity;
import com.zjff.mobiletwo.service.IAddressService;
import com.zjff.mobiletwo.service.IOrderService;
import com.zjff.mobiletwo.service.IcartService;
import com.zjff.mobiletwo.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private IcartService cartService;
    @Override
    public String create(Integer aid, Integer uid, String username, Integer[] cids) {
        //即将下单
        List<CartVO> list = cartService.getVOByCid(uid, cids);
        //计算商品总价
        Long totalPrice = 0L;
        for (CartVO c: list){
            totalPrice += c.getRealPrice() * c.getNum();
        }
        AddressEntity addressEntity = addressService.getByAid(aid,uid);
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setUid(uid);
        //收货地址数据
        orderEntity.setRecvName(addressEntity.getName());
        orderEntity.setRecvPhone(addressEntity.getPhone());
        orderEntity.setRecvProvince(addressEntity.getProvinceName());
        orderEntity.setRecvCity(addressEntity.getCityName());
        orderEntity.setRecvArea(addressEntity.getAreaName());
        orderEntity.setRecvAddress(addressEntity.getAddress());
        //支付、总价
        orderEntity.setStatus(0);
        orderEntity.setTotalPrice(totalPrice);
        orderEntity.setOrderTime(new Date());
        //日志
        orderEntity.setCreatedUser(username);
        orderEntity.setCreatedTime(new Date());
        orderEntity.setModifiedUser(username);
        orderEntity.setModifiedTime(new Date());
        //查询数据
        Integer rows = orderDao.interOrder(orderEntity);
        if (rows != 1){
            return ResultEntity.Fail(-10007,"插入数据时出现未知错误，请联系系统管理员！");
        }
        //创建的订单详细数据
        for (CartVO c: list){
            //创建一个订单项数据
            OrderItemEntity orderItemEntity = new OrderItemEntity();
            orderItemEntity.setOid(orderEntity.getOid());
            orderItemEntity.setPid(c.getPid());
            orderItemEntity.setName(c.getName());
            orderItemEntity.setTitle(c.getTitle());
            orderItemEntity.setPic(c.getPic());
            orderItemEntity.setPrice(c.getRealPrice());
            orderItemEntity.setNum(c.getNum());
            //日志字段
            orderItemEntity.setCreatedUser(username);
            orderItemEntity.setCreatedTime(new Date());
            orderItemEntity.setModifiedUser(username);
            orderItemEntity.setModifiedTime(new Date());
            //插入数据
            rows = orderDao.interOrderItem(orderItemEntity);
            if (rows != 1){
                return ResultEntity.Fail(-10007,"插入数据时出现未知错误，请联系系统管理员！");
            }
        }
        return ResultEntity.Success(null,orderEntity,0,"添加成功!");
    }

    @Override
    public String getOrderByUid(Integer uid) {
        return ResultEntity.Success(null,orderDao.getOrderByUid(uid),0,"获取成功!");
    }

    @Override
    public String getOrderByOid(Integer oid) {
        return ResultEntity.Success(null,orderDao.getOrderByOid(oid),0,"获取成功!");
    }

    @Override
    public String cancelstatus(Integer status) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setStatus(1);
        return null;
    }

    @Override
    public String deleteByOid(Integer oid) {
        return ResultEntity.Success(null,orderDao.deleteByOid(oid),0,"删除成功!");
    }

}
